//
//  YSPPictureView.swift
//  Weibo Demo
//
//  Created by 朴裕识 on 15/12/23.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SDWebImage

class YSPPictureView: UICollectionView {
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    let reuseIdentifier = "PictureViewCellReuseIdentifier"
    
    let layout = UICollectionViewFlowLayout()
    
    var statusModel: YSPStatus? {
        didSet {
            //防止cell复用
            reloadData()
        }
    }

    init() {
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        backgroundColor = UIColor.whiteColor()
        registerClass(YSPPictureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        dataSource = self
        
    }

    func getViewSize() -> CGSize {
        let itemSize = CGSize(width: 90, height: 90)
        
        layout.itemSize = itemSize
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        //获取图片数量
        let count = statusModel?.realPicURLArray?.count ?? 0
        
        if count == 0 {
            //没有图片
            return CGSizeZero
        }
        
        if count == 1 {
            //这是图片默认的大小，因为图片有可能缓存失败
            var size = CGSize(width: 150, height: 120)
            //根据URL拿出缓存的图片的size
            let url = statusModel?.picURLArray?[0]
            if let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(url?.absoluteString) {
                size = image.size
            }
            if size.width < 40 {
                size.width = 40
            }
            else if size.width > UIScreen.mainScreen().bounds.size.width * 0.5 {
                size.width = UIScreen.mainScreen().bounds.size.width * 0.5
            }
            
            layout.itemSize = size
            return size
        }
        
        let margin: CGFloat = 10
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin
        
        if count == 4 {
            let value = 2 * itemSize.width + margin
            return CGSize(width: value, height: value)
        }
        
        // 列数,行数计算依赖列数,column必须为Int
        let column = 3
        
        // 宽度 = 列数 * cell的宽度 + (列数 - 1) * 间距
        let width = CGFloat(column) * itemSize.width + (CGFloat(column) - 1) * margin
        
        // 行数 = (图片的数量(Int) + 列数(Int) - 1) / 列数(Int)
        /*
        1 + 3 - 1 / 3 = 1
        2 + 3 - 1 / 3 = 1
        3 + 3 - 1 / 3 = 1
        5 + 3 - 1 / 3 = 2
        6 + 3 - 1 / 3 = 2
        7 + 3 - 1 / 3 = 3
        8 + 3 - 1 / 3 = 3
        9 + 3 - 1 / 3 = 3
        */
        let row = (count + column - 1) / column
        
        // 高度 = 行数 * cell的高度 + (行数 - 1) * 间距
        let height = CGFloat(row) * itemSize.height + (CGFloat(row) - 1) * margin
        
        return CGSize(width: width, height: height)
    }
    
}


//MARK: - 数据源代理方法
extension YSPPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return statusModel?.realPicURLArray?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! YSPPictureViewCell
        cell.imageURL = statusModel?.realPicURLArray![indexPath.item]
        
        return cell
    }
    
}

//MARK: - Cell
class YSPPictureViewCell: UICollectionViewCell {
    
    var imageURL: NSURL? {
        didSet {
            imageView.sd_setImageWithURL(imageURL)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = UIColor.blueColor()
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI() {
        addSubview(imageView)
        imageView.ff_Fill(contentView)
    }
    
    //MARK: - 懒加载
    private lazy var imageView: UIImageView = {
       let imageView = UIImageView()
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
}












